{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "889f980c",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/data_connectors/ChromaDemo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f3ca56f0-6ef1-426f-bac5-fd7c374d0f51",
   "metadata": {},
   "source": [
    "# Chroma Reader"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "35048096",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5bbbb1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-readers-chroma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03e17a26",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "778ee662",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import sys\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
    "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "262f990a-79c8-413a-9f3c-cd9a3c191307",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.readers.chroma import ChromaReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "252f8163-7297-44b6-a838-709e9662f3d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# The chroma reader loads data from a persisted Chroma collection.\n",
    "# This requires a collection name and a persist directory.\n",
    "\n",
    "reader = ChromaReader(\n",
    "    collection_name=\"chroma_collection\",\n",
    "    persist_directory=\"examples/data_connectors/chroma_collection\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53b49187-8477-436c-9718-5d2f8cc6fad0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# the query_vector is an embedding representation of your query.\n",
    "# Example query vector:\n",
    "#   query_vector=[0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3]\n",
    "\n",
    "query_vector = [n1, n2, n3, ...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a88be1c4-603f-48b9-ac64-10a219af4951",
   "metadata": {},
   "outputs": [],
   "source": [
    "# NOTE: Required args are collection_name, query_vector.\n",
    "# See the Python client: https://github.com/chroma-core/chroma\n",
    "# for more details.\n",
    "documents = reader.load_data(\n",
    "    collection_name=\"demo\", query_vector=query_vector, limit=5\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "169b4273-eb20-4d06-9ffe-71320f4570f6",
   "metadata": {},
   "source": [
    "### Create index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac4563a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import SummaryIndex\n",
    "\n",
    "index = SummaryIndex.from_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f06b02db",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set Logging to DEBUG for more detailed outputs\n",
    "query_engine = index.as_query_engine()\n",
    "response = query_engine.query(\"<query_text>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97d1ae80",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chroma-gpt-index",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  },
  "vscode": {
   "interpreter": {
    "hash": "0ac390d292208ca2380c85f5bce7ded36a7a25670a97c40b8009630eb36cb06e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
